<template>
  <div class="content-area">
    <div class="toolbar">
      <button class="upload-btn" @click="handleUpload">
        <FontAwesomeIcon :icon="['fas', 'cloud-upload-alt']" />
        <span>上传</span>
      </button>
      <button class="new-folder-btn" @click="handleCreateFolder">
        <FontAwesomeIcon :icon="['fas', 'folder']" />
        <span>新建文件夹</span>
      </button>
      <button class="refresh-btn" @click="handleRefresh">
        <FontAwesomeIcon :icon="['fas', 'refresh']" />
        <span>刷新</span>
      </button>
    </div>

    <div class="path-nav">
      <span class="path-item">首页</span>
      <span class="path-separator">/</span>
    </div>

    <div class="file-table">
      <!-- 表头 -->
      <div class="file-header">
        <div class="col-checkbox">
          <input type="checkbox" @change="handleSelectAll" />
        </div>
        <div class="col-name">名称</div>
        <div class="col-size">大小</div>
        <div class="col-time">修改时间</div>
        <div class="col-actions"></div>
      </div>

      <!-- 文件列表 -->
      <div class="file-list">
        <div
          v-for="file in fileList"
          :key="file.id"
          class="file-item"
          @click="handleFileClick(file)"
        >
          <div class="col-checkbox">
            <input type="checkbox" @click.stop />
          </div>
          <div class="col-name">
            <FontAwesomeIcon :icon="['fas', getFileIcon(file.type)]" class="file-icon" />
            <span class="file-name">{{ file.name }}</span>
          </div>
          <div class="col-size">{{ file.size }}</div>
          <div class="col-time">{{ file.time }}</div>
          <div class="col-actions">
            <button @click.stop="handleDeleteFile(file.id)">删除</button>
            <button @click.stop="handleRenameFile(file.id)">重命名</button>
            <button @click.stop="handlePreviewFile(file.id)">预览</button>
            <button @click.stop="handleShareFile(file.id)">分享</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome';
import { FileItem } from '@/stores/counter';

// 定义接收的属性
const props = defineProps<{
  fileList: FileItem[];
}>();

// 定义向外发送的事件
const emit = defineEmits([
  'upload',
  'create-folder',
  'file-click',
  'delete-file',
  'rename-file',
  'preview-file',
  'share-file',
  'refresh'
]);

// 获取文件图标
const getFileIcon = (type: FileItem['type']): string => {
  switch (type) {
    case 'folder':
      return 'folder';
    case 'image':
      return 'image';
    case 'video':
      return 'video';
    case 'music':
      return 'music';
    default:
      return 'file';
  }
};

// 处理上传按钮点击
const handleUpload = () => {
  emit('upload');
};

// 处理新建文件夹点击
const handleCreateFolder = () => {
  emit('create-folder');
};

// 处理刷新按钮点击
const handleRefresh = () => {
  emit('refresh');
};

// 处理文件点击
const handleFileClick = (file: FileItem) => {
  emit('file-click', file);
};

// 处理删除文件
const handleDeleteFile = (fileId: number) => {
  emit('delete-file', fileId);
};

// 处理重命名文件
const handleRenameFile = (fileId: number) => {
  emit('rename-file', fileId);
};

// 处理预览文件
const handlePreviewFile = (fileId: number) => {
  emit('preview-file', fileId);
};

// 处理分享文件
const handleShareFile = (fileId: number) => {
  emit('share-file', fileId);
};

// 处理全选
const handleSelectAll = (event: Event) => {
  const target = event.target as HTMLInputElement;
  const checkboxes = document.querySelectorAll('.file-item .col-checkbox input');
  checkboxes.forEach((checkbox) => {
    (checkbox as HTMLInputElement).checked = target.checked;
  });
};
</script>

<style scoped>
/* 内容区域 */
.content-area {
  flex: 1;
  background-color: rgba(20, 20, 30, 0.6);
  border-radius: 8px;
  padding: 20px;
  backdrop-filter: blur(5px);
  border: 1px solid rgba(134, 3, 174, 0.1);
  box-shadow: 0 0 15px rgba(134, 3, 174, 0.1);
  overflow-y: auto;
}

.toolbar {
  margin-bottom: 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.toolbar button {
  background-color: rgba(30, 30, 50, 0.6);
  color: #ffffff;
  border: 1px solid rgba(134, 3, 174, 0.3);
  border-radius: 4px;
  padding: 8px 15px;
  cursor: pointer;
  display: flex;
  align-items: center;
  transition: all 0.3s;
}

.toolbar button:hover {
  background-color: rgba(134, 3, 174, 0.2);
  border-color: rgba(134, 3, 174, 0.5);
  box-shadow: 0 0 10px rgba(134, 3, 174, 0.3);
}

.toolbar button svg {
  margin-right: 8px;
  color: #a17fe0;
}

.path-nav {
  margin-bottom: 20px;
  color: rgba(255, 255, 255, 0.7);
  display: flex;
  align-items: center;
}

.path-item {
  cursor: pointer;
  transition: color 0.3s;
}

.path-item:hover {
  color: #c084fc;
}

.path-separator {
  margin: 0 8px;
  color: rgba(255, 255, 255, 0.4);
}

/* 文件表格 */
.file-table {
  border-radius: 8px;
  overflow: hidden;
  background-color: rgba(25, 25, 35, 0.4);
  border: 1px solid rgba(134, 3, 174, 0.2);
}

/* 统一表头和数据行的列样式 */
.file-header, .file-item {
  display: grid;
  grid-template-columns: 60px minmax(300px, 1fr) 150px 200px 200px;
  align-items: center;
  width: 100%;
}

.file-header {
  padding: 15px;
  background-color: rgba(30, 30, 50, 0.5);
  font-weight: bold;
  color: rgba(255, 255, 255, 0.9);
  border-bottom: 1px solid rgba(134, 3, 174, 0.2);
}

.file-list {
  max-height: calc(100vh - 250px);
  overflow-y: auto;
}

.file-item {
  padding: 15px;
  border-bottom: 1px solid rgba(134, 3, 174, 0.1);
  color: rgba(255, 255, 255, 0.8);
  transition: all 0.2s;
}

.file-item:hover {
  background-color: rgba(134, 3, 174, 0.1);
}

.file-item:last-child {
  border-bottom: none;
}

/* 列样式 */
.col-checkbox {
  display: flex;
  justify-content: center;
  align-items: center;
}

.col-name {
  display: flex;
  align-items: center;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-icon {
  margin-right: 10px;
  color: #a17fe0;
}

.file-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.col-size, .col-time {
  text-align: center;
}

.col-actions {
  display: flex;
  justify-content: flex-end;
  gap: 5px;
}

.file-item .col-actions {
  opacity: 0;
  transition: opacity 0.3s;
}

.file-item:hover .col-actions {
  opacity: 1;
}

.col-actions button {
  background-color: rgba(30, 30, 50, 0.6);
  color: rgba(255, 255, 255, 0.8);
  border: 1px solid rgba(134, 3, 174, 0.3);
  border-radius: 4px;
  padding: 3px 8px;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.2s;
}

.col-actions button:hover {
  background-color: rgba(134, 3, 174, 0.2);
  color: #ffffff;
}

/* 滚动条样式 */
.file-list::-webkit-scrollbar {
  width: 8px;
}

.file-list::-webkit-scrollbar-track {
  background: rgba(30, 30, 50, 0.3);
  border-radius: 4px;
}

.file-list::-webkit-scrollbar-thumb {
  background: rgba(134, 3, 174, 0.3);
  border-radius: 4px;
}

.file-list::-webkit-scrollbar-thumb:hover {
  background: rgba(134, 3, 174, 0.5);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .file-header, .file-item {
    grid-template-columns: 40px 1fr 80px 110px;
  }
  
  .col-actions {
    position: absolute;
    right: 10px;
    opacity: 1;
  }
  
  .file-item {
    position: relative;
  }
}

@media (max-width: 576px) {
  .file-header, .file-item {
    grid-template-columns: 40px 1fr;
  }
  
  .col-size, .col-time {
    display: none;
  }
}
</style> 